'use strict'

module.exports = angular.module 'libs.commons.session', []
.factory 'SessionService', ($log, $http)->
  $log.log "session service init"
  class SessionService
    constructor: ->
      @ROOT = "ROOT" # $rootScope.root数据, 用于阻止用户不停导出excel

      @TOKEN = "Authorization"
      @USER = "USER"
      @OPEN_MSG = "OPEN_MSG"
      @TOKEN_AUTH = "Authorization"
      @CURRENT_CITY = "CURRENT_CITY"
      @CURRENT_CITY_ID = "CURRENT-CITY-ID"
      @AVATAR = "AVATAR"
      @CITIES = "CITIES"
      @PERMISSIONS = "PERMISSIONS"
      @SYSTEMS = "SYSTEMS"

    setRoot: (root)->
      return if !root?
      window.localStorage[ @ROOT ] = JSON.stringify root
    getRoot: -> JSON.parse window.localStorage[ @ROOT ] if window.localStorage[ @ROOT ]

    setToken: (token)->
      return if !token
      window.localStorage[ @TOKEN ] = token
      $http.defaults.headers.common[ @TOKEN ] = token
      $http.defaults.headers.common[ @TOKEN_AUTH ] = token
    getToken: -> window.localStorage[ @TOKEN ] if window.localStorage[ @TOKEN ]
    deleteToken: ->
      delete window.localStorage[ @TOKEN ]

    setUser: (user)->
      return if !user?
      window.localStorage[ @USER ] = JSON.stringify(user)
    getUser: -> JSON.parse window.localStorage[ @USER ] if window.localStorage[ @USER ]
    deleteUser: -> delete window.localStorage[ @USER ]

    setOpenMsg: (openMsg)->
      return if !openMsg?
      window.localStorage[ @OPEN_MSG ] = JSON.stringify openMsg
    getOpenMsg: -> JSON.parse window.localStorage[ @OPEN_MSG ] if window.localStorage[ @OPEN_MSG ]
    deleteOpenMsg: -> delete window.localStorage[ @OPEN_MSG ]

    setCities: (cities)->
      return if !cities or cities.length <= 0
      window.localStorage[ @CITIES ] = JSON.stringify cities
    getCities: -> JSON.parse window.localStorage[ @CITIES ] if window.localStorage[ @CITIES ]
    deleteCities: -> delete window.localStorage[ @CITIES ]

    setCurrentCity: (currentCity)->
      return if !currentCity?
      window.localStorage[ @CURRENT_CITY ] = JSON.stringify currentCity
      $http.defaults.headers.common[ @CURRENT_CITY_ID ] = currentCity.id
    getCurrentCity: -> JSON.parse window.localStorage[ @CURRENT_CITY ] if window.localStorage[ @CURRENT_CITY ]
    deleteCurrentCity: -> delete window.localStorage[ @CURRENT_CITY ]

    setAvatar: (avatar)->
      return if !avatar
      window.localStorage[ @AVATAR ] = avatar
    getAvatar: -> window.localStorage[ @AVATAR ] if window.localStorage[ @AVATAR ]

    setPermissions: (permissions)-> window.localStorage[@PERMISSIONS] = JSON.stringify permissions
    getPermissions: -> JSON.parse window.localStorage[@PERMISSIONS] if window.localStorage[@PERMISSIONS]
    deletePermissions: -> delete window.localStorage[@PERMISSIONS] if window.localStorage[@PERMISSIONS]

    setSystems: (systems)-> window.localStorage[@SYSTEMS] = JSON.stringify systems
    getSystems: -> JSON.parse window.localStorage[@SYSTEMS] if window.localStorage[@SYSTEMS]
    deleteSystems: -> delete window.localStorage[@SYSTEMS] if window.localStorage[@SYSTEMS]

    initHeaders: ->
      $http.defaults.headers.common[ @TOKEN ] = if @getToken() then @getToken() else ""
      $http.defaults.headers.common[ @TOKEN_AUTH ] = if @getToken() then @getToken() else ""
      $http.defaults.headers.common[ @CURRENT_CITY_ID ] = @getCurrentCity().id if @getCurrentCity()?

    initTokenHeader: ->
      return false if !(token = @getToken())?
      $http.defaults.headers.common[ @TOKEN ] = token
      $http.defaults.headers.common[ @TOKEN_AUTH ] = token
      return true
